package com.intellij.ide.license.idea;

import com.intellij.ide.license.impl.LicenseFile;
import com.intellij.licensecommon.data.LicenseData;
import com.intellij.licensecommon.data.LicenseKeyDecoder;
import com.intellij.licensecommon.data.SubscriptionLicense;
import com.intellij.licensecommon.data.SubscriptionLicenseDataImpl;
import com.intellij.licensecommon.util.LicenseDecoder;
import com.intellij.licensecommon.util.LicenseInfo;
import com.intellij.licensecommon.util.SubscriptionLicenseInfo;
import com.intellij.licensecommon.util.a;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

class IdeaLicense extends LicenseFile {
    @NonNls
    private static final String SYSTEM_PATH_LICENSE = PathManager.getSystemPath() + File.separatorChar + "idea.license";
    @NonNls
    private static final String BIN_PATH_LICENSE = PathManager.getBinPath() + File.separatorChar + "idea.license";
    private static final String SUBSCRIPTION_PREFIX = "<subscription>\n";
    public static final LicenseKeyDecoder<SubscriptionLicenseInfo> decoder = new LicenseKeyDecoder() {
        public SubscriptionLicenseInfo decodeLicenseKey(String licenseKey, String username) throws a {
            LicenseInfo locale = LicenseDecoder.decodeLicenseKey(username, licenseKey);
            if (!(locale instanceof SubscriptionLicenseInfo)) {
                throw new a();
            }
            return (SubscriptionLicenseInfo) locale;
        }
    };

    protected IdeaLicense(String paramString1, String paramString2) {
        super(paramString1, paramString2);
    }

    public static LicenseFile create() {
        return new IdeaLicense(getLicensePath(), getCurrentVersionLicensePath());
    }

    private static String getLicensePath() {
        for (int j = 13; j >= 4; j--) {
            String path = getLicensePathByVersion(j);
            if (new File(path).exists()) {
                return path;
            }
        }
        if (new File(SYSTEM_PATH_LICENSE).exists()) {
            return SYSTEM_PATH_LICENSE;
        }
        return BIN_PATH_LICENSE;
    }

    private static String getLicensePathByVersion(int paramInt) {
        String str = PathManager.getConfigPath() + File.separatorChar + "idea" + paramInt;
        if (paramInt <= 9) {
            str = str + "0";
        }
        str = str + ".key";
        return str;
    }

    private static String getCurrentVersionLicensePath() {
        return getLicensePathByVersion(13);
    }

    @Nullable
    @Override
    protected LicenseData b(String paramString) {
        if (paramString.startsWith(SUBSCRIPTION_PREFIX)) {
            localObject = paramString.substring(SUBSCRIPTION_PREFIX.length());
            return b.a((String) localObject, decoder);
        }
        Pair<String, String> localObject = a(paramString);
        if (localObject == null) {
            return null;
        }
        String str1 = localObject.getFirst();
        String str2 = localObject.getSecond();
        if (isServerUrlStoredInOldFormat(str2, str1)) {
            return null;
        }
        return new SubscriptionLicenseDataImpl(str2, str1);
    }

    @Override
    public int getLicenseType() {
        return 1;
    }

    @Override
    protected void writeTo(LicenseData licenseData, OutputStream outputStream)
            throws IOException {
        if ((licenseData instanceof SubscriptionLicense)) {
            write(outputStream, SUBSCRIPTION_PREFIX);
            write(outputStream, licenseData.getUsername());
            write(outputStream, "\n");
            write(outputStream, licenseData.getKey());
        } else {
            write(outputStream, licenseData.getKey());
            write(outputStream, "\n");
            write(outputStream, licenseData.getUsername());
        }
    }
}
